<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card>
      <!-- 搜索与添加区 -->
      <el-row :gutter="20">
        <el-col :span="10">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- table表格区 -->
      <el-table :data="goodslist" border stripe>
        <!-- 索引列 -->
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column
          label="商品价格(元)"
          prop="goods_price"
          width="95px"
        ></el-table-column>
        <el-table-column
          label="商品重量"
          prop="goods_weight"
          width="70px"
        ></el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="140px">
          <template v-slot="scope">
            {{ scope.row.add_time | dataFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template v-slot="scope">
            <!-- <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editGood(scope.row)"
              >编辑</el-button
            > -->
            <el-button
            class="tableBtn"
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeGood(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="this.queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total"
      >
      </el-pagination>
    </el-card>
    <!-- 修改商品对话框 -->
    <!-- <el-dialog
      title="修改商品信息"
      :visible.sync="editGoodDialogVisible"
      width="50%"
    >
      <el-form
        :model="editGoodInfo"
        :rules="editGoodFormRules"
        ref="addGoodFormRef"
        label-width="80px"
      > -->
        <!-- 修改商品内容主体区域 -->
        <!-- <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editGoodInfo.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="goods_price">
          <el-input v-model="editGoodInfo.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="数量" prop="goods_number">
          <el-input v-model="editGoodInfo.goods_number"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model="editGoodInfo.goods_weight"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editGoodDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editGoodData">确 定</el-button>
      </span>
    </el-dialog> -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      //控制添加商品对话框的显示隐藏
      addGoodDialogVisible: false,
      //查询商品的数据
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5,
      },
      //添加商品输入框的信息
      addGoodForm: {
        goods_name: '',
        goods_cat: '',
        goods_price: '',
        goods_number: '',
        goods_weight: '',
        pics: '',
        attrs: [],
      },
      //控制编辑商品对话框的显示隐藏
      editGoodDialogVisible: false,
      //编辑商品的验证规则
      editGoodFormRules: {
        goods_name: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
        goods_price:[{ required: true, message: '请输入商品价格', trigger: 'blur' }],
        goods_number:[{ required: true, message: '请输入商品数量', trigger: 'blur' }],
        goods_weight:[{ required: true, message: '请输入重量', trigger: 'blur' }]
      },
      //获取到的商品列表数据
      goodslist: [],
      //要编辑的商品数据
      editGoodInfo: {
        goods_id:'',
        goods_name: '',
        goods_price:'',
        goods_number:'',
        goods_weight:'',
      },
      //总数据条数
      total: 0,
      //添加商品验证规则
      addGoodsFormRules: {},
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    //获取商品数据列表
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo,
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品数据失败')
      }
      this.goodslist = res.data.goods
      this.total = res.data.total
    },
    //监听添加商品对话框的关闭事件
    addGoodDialogClosed() {
      this.$refs.addGoodsFormRef.resetFields()
    },
    //监听页面显示数据条数的变化
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    //监听当前页码的变化
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    //删除指定商品
    async removeGood(row) {
      const confirmResult = await this.$confirm(
        '此操作将永久删除该商品, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)
      if (confirmResult !== 'confirm') return
      const { data: res } = await this.$http.delete(`goods/${row.goods_id}`)
      if (res.meta.status !== 200) {
        this.$message.error('删除失败')
      }
      this.$message.success('删除成功')
      this.getGoodsList()
    },
    //点击添加商品按钮，转到添加商品页面
    goAddPage() {
      this.$router.push('/goods/add')
    },
    //编辑商品按钮点击事件
    editGood(row) {
      this.editGoodDialogVisible= true
      console.log(row);
      this.editGoodInfo.goods_id = row.goods_id
      this.editGoodInfo.goods_name = row.goods_name
      this.editGoodInfo.goods_price =row.goods_price
      this.editGoodInfo.goods_number = row.goods_number
      this.editGoodInfo.goods_weight = row.goods_weight
    },
    //编辑商品按钮的确认
   async editGoodData() {
     console.log(this.editGoodInfo);
      const {data:res} = await this.$http.put('goods/'+this.editGoodInfo.goods_id,{
        goods_name: this.editGoodInfo.goods_name,
        goods_price:this.editGoodInfo.goods_price,
        goods_number:this.editGoodInfo.goods_number,
        goods_weight:this.editGoodInfo.goods_weight,
      })
      if(res.meta.status!==200){
        console.log(res);
        return this.$message.error('编辑商品信息失败')
      }
      this.$message.success('编辑商品信息成功')
          this.getGoodsList()
          },
    //关闭窗口前的验证
    editGoodDialogClosed(){
      this.$refs.editGoodsFormRef.resetFields()
    },
  },
}
</script>

<style lang="less" scoped>
.tableBtn{
  margin-left: 30px;
}
</style>